package com.eos.business.service;

import java.util.ArrayList;
import java.util.List;

import com.eos.common.tool.ConstantTool;
import com.eos.common.tool.DaoAccessObjectPoolContextFactory;
import com.eos.orm.mybatis.dao.RoleMappMenuMapper;
import com.eos.orm.mybatis.dao.SysMenuMapper;
import com.eos.orm.mybatis.dao.SysRoleMapper;
import com.eos.orm.mybatis.dao.SysUserMapper;
import com.eos.orm.mybatis.dao.ViewRoleMenuMapper;
import com.eos.orm.mybatis.model.RoleMappMenu;
import com.eos.orm.mybatis.model.SysMenu;
import com.eos.orm.mybatis.model.SysMenuExample;
import com.eos.orm.mybatis.model.SysRole;
import com.eos.orm.mybatis.model.SysRoleExample;
import com.eos.orm.mybatis.model.SysUser;
import com.eos.orm.mybatis.model.SysUserExample;
import com.eos.orm.mybatis.model.ViewRoleMenu;
import com.eos.orm.mybatis.model.ViewRoleMenuExample;

public class SysUserBusinessService extends AbstractEosService<SysUser> {

	private SysUserMapper mapper = (SysUserMapper) DaoAccessObjectPoolContextFactory
			.getInstaAccessObjectPoolContext().instanceDataAccessObject("SysUserMapper");// 通过这种方式获取orm_dao_bean.xml中配置的dao层对象
	private SysMenuMapper sysMenuMapper = (SysMenuMapper) DaoAccessObjectPoolContextFactory
			.getInstaAccessObjectPoolContext().instanceDataAccessObject("SysMenuMapper");
	private SysRoleMapper roleMapper = (SysRoleMapper)DaoAccessObjectPoolContextFactory
		.getInstaAccessObjectPoolContext().instanceDataAccessObject("SysRoleMapper");
	private ViewRoleMenuMapper viewRoleMenuMapper = (ViewRoleMenuMapper)DaoAccessObjectPoolContextFactory
		.getInstaAccessObjectPoolContext().instanceDataAccessObject("ViewRoleMenuMapper");
	private RoleMappMenuMapper roleMappMenuMapper = (RoleMappMenuMapper) DaoAccessObjectPoolContextFactory
		.getInstaAccessObjectPoolContext().instanceDataAccessObject("RoleMappMenuMapper");

	@Override
	public SysUser findById(int id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public String save(SysUser t) {
		int flag = mapper.insert(t);
		return 0 == flag ? "SUCCESS" : "FAIL";
	}

	@Override
	public String update(SysUser user){
		int flag = mapper.updateByPrimaryKeySelective(user);
		return flag == 0 ? "SUCCESS" : "FAIL";
	}
	
	public List<SysUser> findSysUserByCondition(SysUser user) {
		List<SysUser> userList = null;
		if (user == null)
			return userList;
		SysUserExample example = new SysUserExample();
		SysUserExample.Criteria criteria = example.createCriteria();
		if (null != user.getUserEmail())
			criteria.andUserEmailEqualTo(user.getUserEmail());
		if (null != user.getDealFlag())
			criteria.andDealFlagEqualTo(user.getDealFlag());
		if(null != user.getPassWord())
			criteria.andPassWordEqualTo(user.getPassWord());
		example.setOrderByClause(" id desc ");
		userList = mapper.selectByExample(example);
		return userList;
	}

	/**
	 * 保存菜单
	 * @param menu
	 * @return
	 */
	public String saveSysMenu(SysMenu menu){
		return sysMenuMapper.insert(menu) == 0 ? "SUCCESS" : "FAIL";
	}
	
	/**
	 * 修改菜单的指定字段信息
	 * @param menu
	 * @return
	 */
	public String updateSysMenuSelective(SysMenu menu){
		return sysMenuMapper.updateByPrimaryKeySelective(menu) == 0 ? "SUCCESS" : "FAIL";
	}
	
	/**
	 * 查找系统所有有效菜单
	 * @return
	 */
	public List<SysMenu> findAllSysMenu() {
		List<SysMenu> menuList = new ArrayList<SysMenu>();
		SysMenuExample example = new SysMenuExample();
		SysMenuExample.Criteria cri = example.createCriteria();
		cri.andDealFlagEqualTo(ConstantTool.DEAL_FALG_OK);
		cri.andStatusEqualTo(ConstantTool.STATUS_OK);
		menuList = sysMenuMapper.selectByExample(example);
		return menuList;
	}
	
	/**
	 * 新增角色
	 * @param role
	 * @return
	 */
	public String saveRole(SysRole role){
		int flag =  roleMapper.insert(role);
		return flag == 0 ? "SUCCESS" : "FAIL";
	}
	
	/**
	 * 查找所有有效的角色
	 * @return
	 */
	public List<SysRole> findAllSysRole(){
		SysRoleExample example = new SysRoleExample();
		SysRoleExample.Criteria cri = example.createCriteria();
		cri.andDealFlagEqualTo(ConstantTool.DEAL_FALG_OK);
		example.setOrderByClause(" id desc ");
		return roleMapper.selectByExample(example);
	}
	
	public String updateSysRoleSelective(SysRole role){
		SysRoleExample example = new SysRoleExample();
		SysRoleExample.Criteria cri = example.createCriteria();
		cri.andIdEqualTo(role.getId());
		return roleMapper.updateByExampleSelective(role, example) == 0 ? "SUCCESS" : "FAIL";
	}
	
	/**
	 * 更新角色信息
	 * @param role
	 * @return
	 */
	public String updateSysRoleByPrimaryKeySelective(SysRole role){
		return roleMapper.updateByPrimaryKeySelective(role)==0?"SUCCESS" : "FAIL";
	}
	
	/**
	 * 查找角色菜单视图
	 * @return
	 */
	public List<ViewRoleMenu> findAllRoleMenu(){
		ViewRoleMenuExample example = new ViewRoleMenuExample();
		return viewRoleMenuMapper.selectByExample(example);
	}
	
	/**
	 * 批量新增角色菜单关系
	 * @param mappList
	 * @return
	 */
	public String batchSaveRoleMappMenu(List<RoleMappMenu> mappList){
		//TODO
		for(RoleMappMenu rmm : mappList){
			roleMappMenuMapper.insert(rmm);
		}
		return "SUCCESS";
	}
	
	/**
	 * 批量删除角色菜单关系
	 * @param mappList
	 * @return
	 */
	public String batchDeleteRoleMappMenu(List<RoleMappMenu> mappList){
		//TODO
		if(null != mappList && mappList.size() > 0){
			for(RoleMappMenu rmm : mappList){
				roleMappMenuMapper.deleteByPrimaryKey(rmm.getId());
			}
		}
		return "SUCCESS";
	}
}
